package org.komarichin.struts2;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.komarichin.jpa.entity.IUser;
import org.komarichin.jpa.entity.IRole;
import org.komarichin.struts2.beans.ICustomer;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 19.09.2009
 * Time: 21:02:10
 */
public class BaseAction extends ActionSupport {
    private static final long serialVersionUID = 4707496434599619505L;
    public static final String NAME = "name";
    public static final String LOGIN = "login";
    public static final String PASSWORD = "password";
    public static final String LIST = "list";
    public static final String BACK = "back";
    public static final String CUSTOMER_KEY = "org.komarichin.CUSTOMER_KEY";;


    private String pageTitle = "JDSM";
    private String pageDescription = StringUtils.EMPTY;
    private String pageKeyWords = StringUtils.EMPTY;

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    public boolean isLogged() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        return session != null && session.get(CUSTOMER_KEY) != null;
    }

    public ICustomer getCustomer() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        ICustomer user = null;
        if (session != null && session.get(CUSTOMER_KEY) != null && session.get(CUSTOMER_KEY) instanceof ICustomer) {
            user = (ICustomer)session.get(CUSTOMER_KEY);
        }
        return user;
    }



    public String getPageTitle() {
        return pageTitle;
    }

    public String getPageDescription() {
        return pageDescription;
    }

    public String getPageKeyWords() {
        return pageKeyWords;
    }

    public void setPageTitle(String pageTitle) {
        this.pageTitle = pageTitle;
    }

    public void setPageDescription(String pageDescription) {
        this.pageDescription = pageDescription;
    }

    public void setPageKeyWords(String pageKeyWords) {
        this.pageKeyWords = pageKeyWords;
    }

    public boolean isBack() {
        return StringUtils.isNotEmpty(getRequestParamByName("back"));
    }

    public boolean isConfirm() {
        return StringUtils.isNotEmpty(getRequestParamByName("confirm"));
    }

    public boolean isCancel() {
        return StringUtils.isNotEmpty(getRequestParamByName("cancel"));
    }

    public boolean isPerform() {
        return StringUtils.isNotEmpty(getRequestParamByName("perform"));
    }

    public String getRequestParamByName(String name) {
        return ServletActionContext.getRequest().getParameter(name);
    }
}
